package com.ruoyi.web.controller.ring.app;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ring.domain.RingGroupMessage;
import com.ruoyi.ring.domain.dto.ApplyGroupDTO;
import com.ruoyi.ring.domain.dto.ApprovalDTO;
import com.ruoyi.ring.domain.dto.MemberRemarkDTO;
import com.ruoyi.ring.domain.dto.RingGroupDTO;
import com.ruoyi.ring.domain.vo.RingGroupMessageResultVO;
import com.ruoyi.ring.service.IAppGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 群组 app
 */
@Slf4j
@RestController
@RequestMapping("/ring/app_group")
public class AppGroupController extends BaseController
{
    @Resource
    private IAppGroupService appGroupService;

    /**
     * 新增群组 app
     */
    @PostMapping("/add-group")
    public AjaxResult addGroup(@RequestBody RingGroupDTO dto){
        long userId = SecurityUtils.getUserId();
        log.info("新增群组addGroup: userId={}, param={}", userId, JSON.toJSONString(dto));
        return toAjax(appGroupService.addGroup(dto));
    }
    /**
     * 查询群组列表 app
     */
    @GetMapping("/query-group-list")
    public AjaxResult queryGroupList()
    {
        return AjaxResult.success(appGroupService.queryGroupList());
    }

    /**
     * 获取群组详情 app
     * @param id 群组ID
     */
    @PutMapping("/query-group")
    public AjaxResult queryGroup(Long id)
    {
        return AjaxResult.success(appGroupService.queryGroup(id));
    }

    /**
     * 修改群组显示顺序 app
     * @param id 群组ID
     */
    @PutMapping("/edit-group-sort")
    public AjaxResult editGroupSort(Long id)
    {
        return toAjax(appGroupService.editGroupSort(id));
    }

//    /**
//     * 添加群组成员 app
//     */
//    @PostMapping("/add-group-member")
//    public AjaxResult addGroupMember(@RequestBody ApplyGroupDTO applyGroupDTO)
//    {
//        return toAjax(appGroupService.addGroupMember(applyGroupDTO));
//    }
    /**
     * 申请/邀请加入群组 app
     */
    @PostMapping("/apply-join-group")
    public AjaxResult applyJoinGroup(@RequestBody ApplyGroupDTO applyGroupDTO){
        long userId = SecurityUtils.getUserId();
        log.info("申请/邀请加入群组applyJoinGroup: userId={}, param={}", userId, JSON.toJSONString(applyGroupDTO));
        return toAjax(appGroupService.applyJoinGroup(applyGroupDTO));
    }

    /**
     * 获取群组码ById app
     * @param id 群组ID
     */
    @GetMapping(value = "/query-group-code-id/{id}")
    public AjaxResult queryGroupCodeById(@PathVariable("id") Long id)
    {
        return AjaxResult.success(appGroupService.queryGroupCodeById(id));
    }

    /**
     * 获取群组码 app
     */
    @GetMapping(value = "/query-group-code")
    public AjaxResult queryGroupCode()
    {
        return AjaxResult.success(appGroupService.queryGroupCode());
    }

    /**
     * 查询我邀请的群组消息列表 app
     */
    @GetMapping("/query-inviter-message-list")
    public TableDataInfo queryInviterMessageList(RingGroupMessage ringGroupMessage)
    {
        startPage();
        List<RingGroupMessageResultVO> list = appGroupService.queryInviterMessageList(ringGroupMessage);
        return getDataTable(list);
    }

    /**
     * 查询我申请的群组消息列表 app
     */
    @GetMapping("/query-apply-message-list")
    public TableDataInfo queryApplyMessageList(RingGroupMessage ringGroupMessage)
    {
        startPage();
        List<RingGroupMessageResultVO> list = appGroupService.queryApplyMessageList(ringGroupMessage);
        return getDataTable(list);
    }

    /**
     * 是否同意加入群组 app
     */
    @PutMapping("/approval")
    public AjaxResult approval(@RequestBody ApprovalDTO dto) {
        long userId = SecurityUtils.getUserId();
        log.info("是否同意加入群组approval: userId={}, param={}", userId, JSON.toJSONString(dto));
        return toAjax(appGroupService.approval(dto));
    }

    /**
     * 修改成员备注 app
     */
    @PutMapping("/edit-group-member-remark")
    public AjaxResult editGroupMemberRemark(MemberRemarkDTO dto){
        long userId = SecurityUtils.getUserId();
        log.info("修改备注editGroupMemberRemark: userId={}, param={}", userId, JSON.toJSONString(dto));
        return toAjax(appGroupService.editGroupMemberRemark(dto));
    }
    /**
     * 解散群组 app
     * @param id 群组ID
     * @return
     */
    @DeleteMapping("/remove-group/{id}")
    public AjaxResult removeGroup(@PathVariable("id") Long id)
    {
        return toAjax(appGroupService.removeGroup(id));
    }
    /**
     * 移除群组成员/退出群组 app
     * @param id 群组成员ID
     * @return
     */
    @DeleteMapping("/remove-group-member/{id}")
    public AjaxResult removeGroupMember(@PathVariable("id") Long id)
    {
        return toAjax(appGroupService.removeGroupMember(id));
    }
    /**
     * 查询群组成员列表 app
     * @param id 群组ID
     * @return
     */
    @GetMapping("/query-group-member-list")
    public AjaxResult queryGroupMemberList(Long id)
    {
        return AjaxResult.success(appGroupService.queryGroupMemberList(id));
    }


    /**
     * 身体活力 app
     * @param groupId 群组ID
     * @return
     */
    @GetMapping("/query-physical-vitality")
    public AjaxResult queryPhysicalVitality(Long groupId)
    {
        return AjaxResult.success(appGroupService.queryPhysicalVitality(groupId));
    }

    /**
     * 睡眠 app
     * @param groupId 群组ID
     * @return
     */
    @GetMapping("/query-sleep")
    public AjaxResult querySleep(Long groupId)
    {
        return AjaxResult.success(appGroupService.querySleep(groupId));
    }

    /**
     * 能量平衡 app
     * @param groupId 群组ID
     * @return
     */
    @GetMapping("/query-energy-balance")
    public AjaxResult queryEnergyBalance(Long groupId)
    {
        return AjaxResult.success(appGroupService.queryEnergyBalance(groupId));
    }

}
